﻿using System;
using System.ComponentModel;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

namespace QA.TFS.Rules.Validation
{

  [DisplayName("Compare a ContextParameter To DataField")]
  [Description("Comparison of Context Param to Data Field")]
  public class CompareContextParameterToDataField : ValidationRule
  {

    private string contextVariableName;
    private string dataFieldValue;

    [Description("Context Variable Name to compare")]
    public string ContextVariableName
    {
      get { return contextVariableName; }
      set { contextVariableName = value; }
    }

    [Description("Data Field Value to compare")]
    public string DataFieldValue
    {
      get { return dataFieldValue; }
      set { dataFieldValue = value; }
    }

    [DisplayName("Compare a ContextParameter To DataField")]
    [Description("Comparison of Context Param to Data Field")]
    public override void Validate(object sender, ValidationEventArgs e)
    {
      if (!e.WebTest.Context.ContainsKey(contextVariableName))
      {
        e.IsValid = false;
        e.Message = string.Format(CultureInfo.CurrentCulture, "La variable de contexto {0} no existe!", e.WebTest.Context[contextVariableName], DataFieldValue);
        return;
      }

      if (((string)e.WebTest.Context[contextVariableName]).Equals(dataFieldValue, StringComparison.InvariantCultureIgnoreCase))
      {
        e.IsValid = true;
      }
      else
      {
        e.IsValid = false;
        e.Message = string.Format(CultureInfo.CurrentCulture, "La variable de contexto {0} y {1} no coinciden!", e.WebTest.Context[contextVariableName], DataFieldValue);
      }
    }
  }
}
